﻿#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
         #define PLScannerClearErr(Param) OiCAlarm("ERR" + QString::number(Param))
         #define PLScannerReportErr(Param) OiAlarm() << "ERR" << Param << ":" << ErrorStr[Param]

        class PLScannerErrorImpl : public Impl<PLScannerError>
        {
        public:
            QMap<int, bool> _errorMap;
            QMutex _mutex;
        };

        PLScannerError::PLScannerError()
        {
            OI_I(PLScannerError);
        }

        PLScannerError* PLScannerError::instance()
        {
            static PLScannerError error;
            return &error;
        }

        void PLScannerError::reportError(const ErrorCode error)
        {
            OI_F(PLScannerError);

            QMutexLocker lock(&d->_mutex);
            if(!d->_errorMap.contains(error)
                || !d->_errorMap[error])
            {
                PLScannerReportErr(error); 
                d->_errorMap[error] = true; 
            }
        }

        void PLScannerError::clearError(const ErrorCode error)
        {
            OI_F(PLScannerError);

            QMutexLocker lock(&d->_mutex);

            if(d->_errorMap.contains(error))
            {
                PLScannerClearErr(error); 
                d->_errorMap[error] = false; 
            }
        }

    }
}